<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="${ctx}/css/common.css" />
<link rel="stylesheet" href="${ctx}/css/validate.css" />
<link rel="stylesheet" href="${ctx}/css/dialog/skin/bluebar/ymPrompt.css" />

<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/dialog/ymPrompt.js"></script>

<script language="javascript">
<c:if test="${action == 'SaveAdd'}">
ymPrompt.succeedInfo("<s:text name='AddAccountSuccess' />！",null,null,null,refresh);
</c:if>
<c:if test="${action == 'AccountAlreadyExists'}">
ymPrompt.succeedInfo("<s:text name='AccountAlreadyExists' />！",null,null,null,refresh);
</c:if>
<c:if test="${action == 'SaveClose'}">
ymPrompt.succeedInfo("<s:text name='AddAccountSuccess' />！",null,null,null,close);
</c:if>
<c:if test="${action == 'Update'}">
ymPrompt.succeedInfo("<s:text name='UpdateAccountSuccess' />！",null,null,null,close);
</c:if>

$(document).ready(function() {
	$("#app").focus();
});

//对话框回调方法
function close(tp){
	if(tp=="ok"){
		$("#mainFrame",window.parent.document).attr("src","accountManagerPage.action");
		parent.ymPrompt.close();
	}
}

//关闭对话框
function closeWin(){
	parent.ymPrompt.close();
}

//刷新页面
function refresh(tp){
	if(tp=="ok"){
		$("#mainFrame",window.parent.document).attr("src","accountManagerPage.action");
		window.location.href="editAccountPage.action";
	}
}

function formSubmit(action){
	if(checkForm()){
		if(action == 1) { //保存新增
			$("#action").val("SaveAdd");
		} else if(action == 2){ //保存关闭
			$("#action").val("SaveClose");
		}
		$("#submitForm").submit();
	} else {
		return false;
	}
}

function checkForm(){
	var appFlag = true;
	var accountNameFlag = true;
	var passwordFlag = true;

	if($("#app").val() == "0"){
		$("#app").addClass("error");
		$("#appErrMsg").text("<s:text name='SelectApp' />");
		appFlag = false;
	} else {
		$("#app").removeClass("error");
		$("#appErrMsg").text("*");
		appFlag = true;
	}
	
	if($("#accountName").val().replace(/\s+/g,'') == ""){
		$("#accountName").addClass("error");
		$("#accountNameErrMsg").text("<s:text name='AccountNameIsNotNull' />");
		accountNameFlag = false;
	} else {
		$("#accountName").removeClass("error");
		$("#accountNameErrMsg").text("*");
		accountNameFlag = true;
	}
	
	<c:if test="${accountVo == null}">
	if($("#password").val().replace(/\s+/g,'') == ""){
		$("#password").addClass("error");
		$("#passwordErrMsg").text("<s:text name='PasswordIsNotNull' />");
		passwordFlag = false;
	} else {
		$("#password").removeClass("error");
		$("#passwordErrMsg").text("*");
		passwordFlag = true;
	}
	</c:if>
	
	if(appFlag && accountNameFlag && passwordFlag){
		return true;
	} else {
		return false;
	}
}
</script>
</head>

<body>
<c:if test="${accountVo == null}">
<form id="submitForm" name="submitForm" action="addAccount.action" method="post" onsubmit="return $('#action').val() == 'SaveAdd'?formSubmit(1):formSubmit(2)">
</c:if>
<c:if test="${accountVo != null}">
<form id="submitForm" name="submitForm" action="updateAccount.action" method="post" onsubmit="return formSubmit(2)">
</c:if>
	<input id="id" name="id" type="hidden" value="${accountVo.id }" />
	<input id="action" name="action" type="hidden" value="${action }" />
	<table width="100%" cellspacing="1" cellpadding="5" class="table_class">
		<tr>
			<td width="20%"><s:text name='App' />：</td>
			<td>
				<select id="app" name="app">
					<c:if test="${accountVo != null }">
					<option value="0"><s:text name='SelectApp' /></option>
					<c:forEach var="app" items="${applications}">
						<c:if test="${accountVo.app.id == app.id }">
						<option value="${app.id }" selected="selected">${app.appName }</option>
						</c:if>
						<c:if test="${accountVo.app.id != app.id }">
						<option value="${app.id }">${app.appName }</option>
						</c:if>
					</c:forEach>
					</c:if>
					<c:if test="${accountVo == null }">
					<option value="0" selected="selected"><s:text name='SelectApp' /></option>
					<c:forEach var="app" items="${applications}">
						<option value="${app.id }">${app.appName }</option>
					</c:forEach>
					</c:if>
				</select>
				<span id="appErrMsg" class="inputMsg">*</span>
			</td>
		</tr>
		<tr>
			<td width="20%"><s:text name='AccountName' />：</td>
			<td>
				<input id="accountName" name="accountName" type="text" value="${accountVo.name }" />
				<span id="accountNameErrMsg" class="inputMsg">*</span>
			</td>
		</tr>
		<tr>
			<td width="20%"><s:text name='AccountPassword' />：</td>
			<td>
				<c:if test="${accountVo == null}">
				<input id="password" name="password" type="password" value="" />
				<span id="passwordErrMsg" class="inputMsg">*</span>
				</c:if>
				<c:if test="${accountVo != null}">
				<input id="password" name="password" type="password" readonly="readonly" value="${accountVo.password }" />
				<span id="passwordErrMsg" class="inputMsg">*</span>
				</c:if>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<c:if test="${accountVo == null}">
				<input id="submitBtn" name="submitBtn" type="button" onclick="formSubmit(1);" value="<s:text name='SaveAndAdd' />" />
				<input id="submitBtn" name="submitBtn" type="button" onclick="formSubmit(2);" value="<s:text name='SaveAndClose' />" />
				<input id="submitBtn" name="submitBtn" type="button" onclick="closeWin();" value="<s:text name='Close' />" />
				</c:if>
				<c:if test="${accountVo != null}">
				<input id="submitBtn" name="submitBtn" type="button" onclick="formSubmit(2);" value="<s:text name='Save' />" />
				<input id="submitBtn" name="submitBtn" type="button" onclick="closeWin();" value="<s:text name='Close' />" />
				</c:if>
			</td>
		</tr>
	</table>
</form>
</body>

</html>
